<template>
	<view class="login" v-if="loginView">
		<image src="/static/images/colse.png" @click="colse" class="colse" mode=""></image>
		<view class="mobile" v-show="mobileView">
			<input class="inp" auto-focus type="number" placeholder="请输入手机号" v-model="form.mobile" />
			<view class="radioBox" @click="readingFn">
				<view class="radio" :class="reading ? 'read' : ''">
					<view class="din imgcentent"></view>
				</view>
				<view class="txt">
					<text>我已阅读并同意</text>
					<text class="book">《隐私政策》</text>
					<text>与</text>
					<text class="book">《用户协议》</text>
				</view>
			</view>
			<view class="btn" @click="send">
				{{ sendTxt }}
			</view>
		</view>
		<!--  -->
		<view class="verification" v-show="!mobileView">
			<view class="verificationInpBox">
				<input type="number" class="inp" auto-focus v-model="form.code" maxlength="4" />
			</view>
			<view class="btn" @click="loginCom">立即登录</view>
			<view class="tips" @click="getSms">
				<text class="counting">{{ counting }}</text>
				S 重新获取
			</view>
		</view>
	</view>
</template>

<script>
	import { smsSend } from '@/api/index';
	import { checkPhone } from '@/utils/regular.js';

	export default {
		name: 'login',
		data() {
			return {
				loginView: false,
				mobileView: true,
				counting: 60,
				timer: false,
				sendTxt: '发送验证码',
				reading: false,
				mobile: null,
				IsEnd: false, //倒计时结束
				verificationInp0: null,
				verificationInp1: null,
				verificationInp2: null,
				verificationInp3: null,

				focus1: false,
				focus2: false,
				focus3: false,
				mobileFocus: false,
				form: {
					client: 1,
					mobile: '',
					code: '',
					distribution_code: '',
				},
			};
		},
		methods: {
			getUrlParams() {
				var search = window.location.search;
				var reg = /(?:\?|&)(.*?)=(.*?)(?=&|$)/g;
				var result = {};
				search.replace(reg, function (match, key, value) {
					result[key] = decodeURIComponent(value);
				});
				return result;
			},
			input0(e) {
				this.focus1 = true;
			},
			input1(e) {
				this.focus2 = true;
			},
			input2(e) {
				this.focus3 = true;
			},
			readingFn() {
				this.reading = !this.reading;
			},
			async loginCom() {
				const res = await this.$api.smsLogin({
					...this.form,
				});
				this.showToastMsg(res.msg);

				uni.setStorageSync('token', res.data.token);
				uni.setStorageSync('loginData', res.data);
				this.colse();
				if (res.data.card_business_status == 0) {
					uni.navigateTo({
						url: '/pages/my/card/card',
					});
				} else {
					const pages = getCurrentPages();
					const page = pages[pages.length - 1];
					const fullPath = page.__page__.fullPath;
					uni.reLaunch({
						url: fullPath,
					});
				}
			},
			colse() {
				this.loginView = false;
			},
			open() {
				this.loginView = true;
			},
			async send() {
				if (!checkPhone(this.form.mobile)) return;
				if (!this.reading) {
					this.showToastMsg('请阅读并同意《隐私政策》与《用户协议》');
					return;
				}
				const res = await smsSend({
					mobile: this.form.mobile,
					key: 'YZMDL',
				});
				this.showToastMsg(res.msg);
				this.mobileView = false;
				this.countingFn();
			},
			countingFn() {
				this.timer = setInterval(() => {
					if (this.counting == 0) {
						this.sendText = '重新发送';
						this.IsEnd = true;
						clearInterval(this.timer);
						return;
					}
					this.counting = --this.counting;
				}, 1000);
			},
			getSms() {
				if (this.IsEnd) {
					this.counting = 60;
					this.mobileView = true;
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	.login {
		width: 750rpx;
		height: auto;
		background: #ffffff;
		border-radius: 20rpx 20rpx 0rpx 0rpx;
		position: fixed;
		bottom: 0;
		left: 0;
		z-index: 999;

		.cu-form-group {
			display: flex;
		}

		.colse {
			width: 30rpx;
			height: 30rpx;
			position: absolute;
			top: 35rpx;
			right: 35rpx;
		}

		.mobile {
			overflow: hidden;
			padding-bottom: 140rpx;

			.inp {
				margin: 60rpx auto 50rpx;
				width: 550rpx;
				font-size: $uni-font-size-17;
				padding: 0;
				height: 100rpx;
				// padding-bottom: $submargin-width;
				border-bottom: 1rpx solid #dddddd;
			}

			.radioBox {
				width: auto;
				min-width: 550rpx;
				overflow: hidden;
				margin: 0 auto 40rpx;
				display: flex;
				justify-content: center;

				.radio {
					width: 26rpx;
					height: 26rpx;
					border: 1rpx solid $unselected;
					border-radius: 50%;
					margin-right: 10rpx;

					.din {
						display: none;
					}
				}

				.read {
					border: 1rpx solid $price-color;
					// display: flex;
					// justify-content: center;
					// align-items: center;
					position: relative;

					.din {
						display: block;
						width: 14rpx;
						height: 14rpx;
						background: $price-color;
						border-radius: 50%;
					}
				}

				.txt {
					line-height: 26rpx;
					font-size: $uni-font-size-12;
					color: $placeholder-colcor;

					.book {
						color: $title-colcor;
					}
				}
			}
		}

		.btn {
			width: 550rpx;
			height: 80rpx;
			background: $price-color;
			border-radius: 10rpx;
			color: $txt-color;
			margin: 0 auto;
			text-align: center;
			line-height: 80rpx;
			font-size: $uni-font-size-15;
		}

		.verification {
			overflow: hidden;
			padding-bottom: 90rpx;

			.verificationInpBox {
				display: flex;
				justify-content: center;
				// margin: 124rpx auto 76rpx;

				.verificationInp {
					width: 80rpx;
					height: 80rpx;
					border-radius: 10rpx;
					border: 1rpx solid #707070;
					margin-right: 14rpx;
					text-align: center;
				}

				.inp {
					margin: 60rpx auto 50rpx;
					width: 550rpx;
					font-size: $uni-font-size-17;
					padding: 0;
					height: 100rpx;
					// padding-bottom: $submargin-width;
					border-bottom: 1rpx solid #dddddd;
				}
			}

			.tips {
				text-align: center;
				color: $placeholder-colcor;
				margin-top: 30rpx;
				font-size: $uni-font-size-12;
			}
		}
	}
</style>
